package com.pshcp.doctor.api;

import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.doctor.ao.messagenotificationsettings.SaveDoctorMessageNotificationSettingsAO;
import com.pshcp.doctor.tool.DoctorMessageNotificationSettingsTool;
import com.pshcp.doctor.vo.messagenotificationsettings.DoctorMessageNotificationSettingsVO;
import com.pshcp.domain.model.DoctorMessageNotificationSettings;
import com.pshcp.service.DoctorMessageNotificationSettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/3 21:38
 */
@Api(tags = {"医生端-消息提醒设置接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/messageNotificationSettings")
public class DoctorMessageNotificationSettingsAPI {

    @Autowired
    private DoctorMessageNotificationSettingsService doctorMessageNotificationSettingsService;

    @ApiOperation("获取医生消息提醒设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取医生消息提醒设置")
    @PostMapping("getDoctorMessageNotificationSettings")
    public ResultDTO<DoctorMessageNotificationSettingsVO> getDoctorMessageNotificationSettings() throws BizException {
        DoctorMessageNotificationSettings doctorMessageNotificationSettings = doctorMessageNotificationSettingsService.getDoctorMessageNotificationSettingsByDoctorId(ReqComTool.getUserId());
        if (doctorMessageNotificationSettings == null) {
            DoctorMessageNotificationSettings defaultDoctorMessageNotificationSettings = DoctorMessageNotificationSettingsTool.buildDefualtDoctorMessageNotificationSettings();
            doctorMessageNotificationSettingsService.insert(defaultDoctorMessageNotificationSettings);
            DoctorMessageNotificationSettingsVO defaultDoctorMessageNotificationSettingsVO = CommonTool.copyProperties(defaultDoctorMessageNotificationSettings, DoctorMessageNotificationSettingsVO.class);
            return ResultBuilder.success(defaultDoctorMessageNotificationSettingsVO);
        }
        DoctorMessageNotificationSettingsVO doctorMessageNotificationSettingsVO = CommonTool.copyProperties(doctorMessageNotificationSettings, DoctorMessageNotificationSettingsVO.class);
        return ResultBuilder.success(doctorMessageNotificationSettingsVO);
    }

    @ApiOperation("保存医生消息提醒设置")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "保存医生消息提醒设置")
    @PostMapping("saveDoctorMessageNotificationSettings")
    public ResultDTO<Long> saveDoctorMessageNotificationSettings(@RequestBody SaveDoctorMessageNotificationSettingsAO saveDoctorMessageNotificationSettingsAO) throws BizException {
        DoctorMessageNotificationSettingsTool.checkSaveDoctorMessageNotificationSettings(saveDoctorMessageNotificationSettingsAO);
        DoctorMessageNotificationSettings doctorMessageNotificationSettings = doctorMessageNotificationSettingsService.getDoctorMessageNotificationSettingsByDoctorId(ReqComTool.getUserId());
        if (doctorMessageNotificationSettings == null) {
            DoctorMessageNotificationSettings saveDoctorMessageNotificationSettings = DoctorMessageNotificationSettingsTool.buildSaveDoctorMessageNotificationSettings(saveDoctorMessageNotificationSettingsAO);
            doctorMessageNotificationSettingsService.insert(saveDoctorMessageNotificationSettings);
        } else {
            DoctorMessageNotificationSettings updateDoctorMessageNotificationSettings = DoctorMessageNotificationSettingsTool.buildSaveDoctorMessageNotificationSettings(doctorMessageNotificationSettings, saveDoctorMessageNotificationSettingsAO);
            doctorMessageNotificationSettingsService.update(updateDoctorMessageNotificationSettings);
        }
        return ResultBuilder.success(null);
    }
}
